<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="城市：">
          <city-select v-model="formInline.cities" :multiple="false"></city-select>
        </el-form-item>
        <el-form-item label="部门：">
          <depart-ment v-model="formInline.departmentIds"></depart-ment>
        </el-form-item>
        <el-form-item label="分配时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入姓名或手机"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" :loading="eloading" @click="downloadExcel()">批量导出</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" show-summary :summary-method="getSummaries" :data="tableData">
        <el-table-column prop="departmentName" label="所属部门" show-overflow-tooltip />
        <el-table-column prop="username" label="用户名称" show-overflow-tooltip />
        <el-table-column prop="account" label="手机号码" show-overflow-tooltip />
        <el-table-column prop="totalClueCnt" label="总线索数" show-overflow-tooltip />
        <el-table-column prop="mainProjectClueCnt" label="主诉线索数" show-overflow-tooltip />
        <el-table-column prop="otherProjectClueCnt" label="非主诉线索数" show-overflow-tooltip />
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import { showModal, showToast, deepClone } from "@/common/util";
import {
  GET_ASSIGNMENT_SEARCHSTAT,
  DOWNLOAD_ASSIGNMENT_EXPORTSTAT,
} from "@/http/clues";
import moment from "moment";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,

      summary:{}
    };
  },
  created() {
    let end = moment().format("YYYY-MM-DD");
    let start = moment().startOf('month').format("YYYY-MM-DD");
    this.$set(this.formInline, "orderTime", [start, end]);
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      console.log(oParams);
      _this.loading = true;
      GET_ASSIGNMENT_SEARCHSTAT(oParams).then((res) => {
        console.log(res);
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
        _this.summary = res.summary || {}
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || [];
      if (m && m.length > 0) {
        o.assignStartTime = m[0];
        o.assignEndTime = m[1];
      }

      delete o.orderTime;

      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有分配统计列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};
      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = DOWNLOAD_ASSIGNMENT_EXPORTSTAT;
      options.fileName = "分配统计列表.xlsx";
      _this.eloading = true;
      _this.$exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 获取统计数据
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      let list = ['totalClueCnt', 'mainProjectClueCnt','otherProjectClueCnt'];
      let o = this.summary || {}
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        let idx = list.indexOf(column.property)
        if (idx >= 0) {
          sums[index] = o[column.property]
        } else {
          sums[index] = '';
        }
      });
      return sums;
    }
  },
};
</script>

<style lang="scss" scoped>
</style>
